const HtmlWebpackPlugin = require("html-webpack-plugin"); //通过 npm 安装
const webpack = require("webpack"); //访问内置的插件
var ExtractTextPlugin = require("extract-text-webpack-plugin");
// const UglifyJSPlugin = require("uglifyjs-webpack-plugin");
var helpers = require("./config/helpers");
const CleanWebpackPlugin = require("clean-webpack-plugin");

// http.listen(8000)
// var utils = require("./utils");

const path = require("path");
const museUiThemePath = path.join(
  __dirname,
  "node_modules",
  "muse-ui",
  "src/styles/themes/variables/default.less"
);

const CONFIG = {
  // entry: [
  //   path.resolve(__dirname, "./src/app.js"),
  //   path.resolve(__dirname, "./src/mobile/main.js"),
  //   path.resolve(__dirname, "./public/my-theme/index.less")

  //   // path.resolve(__dirname, "./public/stylesheet/lib.scss")
  // ],
  // entry: entries(utils.root(public) + '/src/*'),
  entry: {
    index: "./src/app.js",
    css: "./public/my-theme/index.less"
  },
  output: {
    // filename: "bundle.js",
    path: helpers.root("dist"),
    publicPath: "/",
    filename: "[name].[hash].js",
    chunkFilename: "[id].[hash].chunk.js"
  },

  cache: true,

  resolve: {
    extensions: [".ts", ".js", ".vue", "scss", "css", ".less"],
    alias: {
      vue: "vue/dist/vue.common.js",
      "muse": 'muse-ui/src',
      src: path.resolve(__dirname, "../src")
    }
  },
  module: {
    rules: [{
        test: /\.css$/,
        use: "css-loader"
      },
      {
        test: /muse-ui.src.*?js$/,
        loader: 'babel-loader'
      },
      // 使用vue-loader 加载 .vue 结尾的文件
      {
        test: /\.vue$/,
        use: [{
          loader: "vue-loader",
          options: {
            loaders: {
              less: ExtractTextPlugin.extract({
                use: [
                  "css-loader?minimize",
                  "autoprefixer-loader",
                  "less-loader",
                  {
                    loader: "less-loader",
                    options: {
                      globalVars: {
                        museUiTheme: `'${museUiThemePath}'`
                      }
                    }
                  }
                ],
                fallback: "vue-style-loader"
              }),
              css: ExtractTextPlugin.extract({
                use: ["css-loader", "autoprefixer-loader", "less-loader"],
                fallback: "vue-style-loader"
              })
            }
          }
        }]

        // loader: "vue-loader",
        // options: {
        //   loaders: {
        //     less: [
        //       "vue-style-loader",
        //       "css-loader",
        //       // {
        //       //   loader: "less-loader",
        //       //   options: {
        //       //     globalVars: {
        //       //       museUiTheme: `'${museUiThemePath}'`
        //       //     }
        //       //   }
        //       // }
        //     ]
        //   }
        // }
      },
      {
        test: /\.js$/,
        // loader: 'babel-loader',
        use: [{
          loader: "babel-loader",
          options: {
            presets: ["es2015"],
          }
        }],
        exclude: /node_modules/
      },
      // {
      //   test: /\.jsx?$/,
      //   loader: "babel-loader",
      //   exclude: /node_modules/,
      //   query: {
      //     presets: ["es2015"]
      //   }
      // },
      {
        test: /\.html$/,
        loader: 'html-loader'
      },
      {
        test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
        loader: "file-loader?name=css/[name].[ext]"
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
          fallbackLoader: "style-loader",
          loader: "css-loader"
        })
      },
      {
        test: /\.less$/,
        loader: "style-loader!css-loader!less-loader",
      }
      // {
      //   test: /\.less/,
      //   use: ExtractTextPlugin.extract({
      //     use: ["css-loader?minimize", "autoprefixer-loader", "less-loader"],
      //     fallback: "less-loader"
      //   })
      // },


      // {

      //   test: /\.(scss|css)$/,

      //   use: ExtractTextPlugin.extract({

      //     use: [

      //       {

      //         loader: "css-loader",

      //         options: {

      //           modules: true,

      //           importLoaders: 1,

      //           localIdentName: "[local]_[hash:base64:5]",

      //           sourceMap: true

      //         }

      //       },

      //       {

      //         loader: "sass-loader",

      //         options: {

      //           sourceMap: true

      //         }

      //       }

      //     ],

      //     fallback: "style-loader"

      //   })

      // }
    ]
  },
  plugins: [
    // new webpack.optimize.UglifyJsPlugin({
    //   compress: {
    //     warnings: false,
    //     drop_debugger: true,
    //     drop_console: true
    //   }
    // }),
    // new UglifyJSPlugin(),
    // new ExtractTextPlugin({
    //   filename: "index.css",
    //   disable: false,
    //   allChunks: true
    // }),

    // new ExtractTextPlugin("[name].css"),
    new ExtractTextPlugin({
      filename: '[name].css',
      allChunks: true
    }),

    // new webpack.optimize.CommonsChunkPlugin({
    //   //common file
    //   name: ["XXapp", "ZZstyle", "app"].reverse()
    // }),
    // new webpack.optimize.UglifyJsPlugin(),
    // new HtmlWebpackPlugin({ template: "./src/index.html" }),

    new HtmlWebpackPlugin({
      filename: "index.html", //http访问路径
      template: "./src/index.html", //实际文件路径
      inject: "body",
      minify: {
        removeComments: true, //压缩html文件
        collapseWhitespace: true,
        removeAttributeQuotes: true
      },
      chunksSortMode: "dependency",
      chunks: ["index", "css"]
    }),
    // new HtmlWebpackPlugin({
    //   filename: "m_index.html", //http访问路径
    //   template: "./src/mobile/index.html", //实际文件路径
    //   inject: "body",
    //   minify: {
    //     removeComments: true, //压缩html文件
    //     collapseWhitespace: true,
    //     removeAttributeQuotes: true
    //   },
    //   chunksSortMode: "dependency",
    //   chunks: ["mindex", "mcss"]
    // }),
    new CleanWebpackPlugin(
      ["dist"], //匹配删除的文件
      {
        root: __dirname, //根目录
        verbose: true, //开启在控制台输出信息
        dry: true //启用删除文件
      }
    ),

    // new HtmlwebpackPlugin({
    //   title: "Hello World app"
    // }), //在build目录下自动生成index.html，指定其title

    // 实现刷新浏览器必写
    new webpack.HotModuleReplacementPlugin(),

    // ...

    new webpack.DefinePlugin({
      "process.env": {
        NODE_ENV: JSON.stringify("production")
      }
    })
  ],

  devServer: {
    compress: true,
    port: 88,
    historyApiFallback: true,
    stats: {
      colors: true
    }, //"minimal",

    hot: true,
    inline: true,
    // contentBase: path.resolve(__dirname, ".public")

    proxy: {
      "/yyhao/*": {
        target: "https://task-globalapi.yyhao.com",

        pathRewrite: {
          "^/yyhao": ""
        },
        secure: false,
        changeOrigin: true
      },
    }
    //   "/qq/music/*": {
    //     target: 'https://c.y.qq.com',
    //     changeOrigin: true,
    //     pathRewrite: {
    //       "^/qq/music": "/"
    //     },

    //   },
    //   "/douban/*": {
    //     target: 'https://api.douban.com',
    //     changeOrigin: true,
    //     pathRewrite: {
    //       "^/douban": "/"

    //     },
    //   }
    // }
  }
};

module.exports = CONFIG;
